/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gracefulmm.dealImage;

import com.taobao.api.TaobaoClient;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import javax.swing.JDialog;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 *
 * @author wenbo
 */
public class UploadImage implements Runnable {
    
    private static final Logger log = Logger.getLogger(UploadImage.class);

    private TaobaoClient taobaoClient = null;
    private String imageTitle = null;
    private String filePath = null;
    private String sessionKey = null;
    private List<String> errorList = null;
    private JDialog dlg;

    public UploadImage(TaobaoClient taobaoClient, String imageTitle,
            String filePath, String sessionKey,List<String> errorList,JDialog dlg) {
        this.taobaoClient = taobaoClient;
        this.imageTitle = imageTitle;
        this.filePath = filePath;
        this.sessionKey = sessionKey;
        this.errorList = errorList;
        this.dlg = dlg;
    }

    @Override
    public void run() {
        //添加图片类目
        log.info("准备添加图片类目!,类目名称为："+imageTitle);
        final Long pictureCategoryId = TaobaoDealUtil.AddPictureCategory(taobaoClient,sessionKey,imageTitle);
         log.info("添加图片类目成功!,id为："+pictureCategoryId);
        List<Future<String>> listfutures = new ArrayList<Future<String>>();
        ExecutorService executorService = ImageExecutorService.getImageExecutorServiceInstance();
        File file = new File(filePath);
        File[] listFiles = file.listFiles();
        if (listFiles != null && listFiles.length > 0) {
            log.info("添加图片类目成功!,id为："+pictureCategoryId);
            for (final File dealFile : listFiles) {
                if (ImageTools.isDeal(dealFile.getName())) {
                    listfutures.add(executorService.submit(new Callable<String>() {
                        @Override
                        public String call() throws Exception {
                            try {
                                if (!TaobaoDealUtil.uploadImg(taobaoClient,sessionKey,pictureCategoryId, dealFile.getPath(),dealFile.getName(),dealFile.getName())) {
                                    return dealFile.getPath();
                                }
                            } catch (Exception e) {
                                log.error("上传文件到图片空间出现错误！文件名为："+dealFile.getPath(), e);
                                return dealFile.getPath();
                            }
                            return "";
                        }
                    }));
                }
            }
        }else{
            log.info("没有需要上传的文件!");
        }
        if (listfutures.size() > 0) {
            for (Future<String> future : listfutures) {
                try {
                    String name = future.get();
                    if (StringUtils.isNotBlank(name)) {
                        this.errorList.add(name);
                    }
                } catch (Exception ex) {
                   log.error("上传图片到图片空间出现异常!", ex);
                }
            }
        } 
        dlg.setVisible(false);
         log.error("上传图片到图片空间完毕!");
    }

}
